< previous page page_402 next page >

Page 402
You can use this fact to speed up vector/matrix multiplication. When you multiply a normalized vector (with 1 as the scale factor in the final component) by this sort of matrix, you can skip calculating the resulting vector's last entry because it is always 1.
The following subroutine uses this fact to multiply a vector by a matrix. It also saves a little time by not using For loops. Writing the calculations out makes the code a little more cluttered, but it also makes it a bit faster. In one series of tests, a similar subroutine that did not use these tricks took about 2.5 times as long to perform the same calculations.
e10a6a0a59f035e870d90cca05b85fce.gif
' Apply a transformation matrix to a point.
Public Sub m2Apply(V() As Single, A() As Single, _
    Result() As Single)

    Result(1) = V(1) * A(1, 1) + V(2) * A(2, 1) + A(3, 1)
    Result(2) = V(1) * A(1, 2) + V(2) * A(2, 2) + A(3, 2)
    Result(3) = 1#
End Sub
If you need to apply a matrix to many points at the same time, you can also speed things up by moving these calculations into the routine transforming the points instead of calling a subroutine. Avoiding all the subroutine calls will make the code a little faster. It is rarely worth complicating code to avoid a subroutine call, but if an application must transform thousands of points, this may be worthwhile.
In the following example, the global variable V is a three-dimensional array that contains the vectors representing the points that make up an object. This routine transforms each of the points in array V and draws lines connecting them. The routine performs all of its calculations directly rather than by calling a subroutine. Because the final components in the transformed points are always 1 and because they are not needed to draw the lines, this routine further increases its speed by ignoring them completely.
e10a6a0a59f035e870d90cca05b85fce.gif
Private Sub DrawPoints()
Dim i As Integer
Dim x As Single
Dim y As Single

    CurrentX = V(1, 1) * A(1, 1) + V(1, 2) * A(2, 1) + A(3, 1)
    CurrentY = V(1, 1) * A(1, 2) + V(1, 2) * A(2, 2) + A(3, 2)
    For i = 2 To NumPoints
        x = V(i, 1) * A(1, 1) + V(i, 2) * A(2, 1) + A(3, 1)
        y = V(i, 1) * A(1, 2) + V(i, 2) * A(2, 2) + A(3, 2)
        Line -(x, y)
    Next i
End Sub
Similarly, you can avoid some calculations when you multiply two matrices. Because you know the last column in the result always contains 0, 0, 1, you do not need to com-

 
< previous page page_402 next page >